export const HOOKS = [
  "beforeCreate",
  "created",
  "beforeMount",
  "mounted",
  "beforeUpdate",
  "updated",
  "beforeDestory",
  "destroyed",
];
//策略模式
let stars = {};
stars.data = function (parentVal, childVal) {
  return childVal;
}; //合并data
//合并组件 把全局的给挂载在原型上
stars.components = function (parentVal, childVal) {
  const obj = Object.create(parentVal);
  if (childVal) {
    for (let key in childVal) {
      obj[key] = childVal[key];
    }
  }
  return obj;
};
// stars.computed = function () {};
// stars.watch = function () {};
// stars.methods = function (parentVal, childVal) {
//   console.log(mergeHook(parentVal, childVal));
//   mergeHook(parentVal, childVal).forEach((cbobj) => {
//     for (const cb in cbobj) {
//       cbobj[cb].call();
//     }
//   });
// };
// stars.data=function(){}
// stars.data=function(){}
//遍历生命周期
HOOKS.forEach((hooks) => {
  stars[hooks] = mergeHook;
});
function mergeHook(parentVal, childVal) {
  // vue.options = {created: [a,b,c],watch: [a,b]}
  if (childVal) {
    if (parentVal) {
      return parentVal.concat(childVal);
    } else {
      return [childVal];
    }
  } else {
    return parentVal;
  }
}
export function mergeOptions(parent, child) {
  // vue.options = {created: [a,b,c],watch: [a,b]}
  const options = {};
  //如果有父亲 没有儿子
  for (const key in parent) {
    mergeField(key);
  }
  //儿子有父亲没有
  for (const key in child) {
    mergeField(key);
  }
  function mergeField(key) {
    // 根据key 策略模式
    if (stars[key]) {
      options[key] = stars[key](parent[key], child[key]);
    } else {
      options[key] = child[key] || parent[key];
    }
  }
  return options;
}
